import type { H3Event } from 'h3'

export enum ResponseCode {
  SUCCESS = 200,    // 请求成功
  BAD_REQUEST = 400, // 请求错误
  UNAUTHORIZED = 401, // 未授权
  FORBIDDEN = 403,  // 禁止访问
  NOT_FOUND = 404,  // 未找到
  CONFLICT = 409,    // 冲突
  INTERNAL_ERROR = 500, // 服务器错误
}

interface ApiResponse<T = any> {
  code: ResponseCode
  message: string
  data?: T
  timestamp: number
  success: boolean
}

export function sendSuccessResponse<T>(event: H3Event, data?: T, message: string = '操作成功'): ApiResponse<T> {
  setResponseStatus(event, ResponseCode.SUCCESS)
  return {
    code: ResponseCode.SUCCESS,
    message,
    data,
    timestamp: Date.now(),
    success: true
  }
}

export function sendErrorResponse(event: H3Event, code: ResponseCode, message: string, errors?: any): ApiResponse {
  setResponseStatus(event, code)
  return {
    code,
    message,
    timestamp: Date.now(),
    success: false,
    ...(errors && { errors })
  }
}